//
//  LDViewController.m
//  CoreData
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 蓝鸥3g学院. All rights reserved.
//

#import "LDViewController.h"
#import "LDCoreDataManager.h"
#import "LDLTView.h"
#import "LDMapViewController.h"
@interface LDViewController ()<LDMapViewControllerDelegate>

@property(nonatomic, strong)LDLTView *name;
@property(nonatomic, strong)LDLTView *sex;
@property(nonatomic, strong)LDLTView *number;
@property(nonatomic, strong)LDLTView *phone;
@property(nonatomic, strong)LDLTView *QQ;
@property(nonatomic, strong)LDLTView *address;
@property(nonatomic, strong)LDCoreDataManager *manager;


@end

@implementation LDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.name = [[LDLTView alloc] initWithFrame:CGRectMake(0, 50, 375, 50)];
    [self.view addSubview:self.name];
    self.name.Lable.text = @"姓名:";
    self.name.TextField.text = self.stu.name;
    
    self.sex = [[LDLTView alloc] initWithFrame:CGRectMake(0, 110, 375, 50)];
    [self.view addSubview:self.sex];
    self.sex.Lable.text = @"性别:";
    self.sex.TextField.text = self.stu.sex;
    
    self.number = [[LDLTView alloc] initWithFrame:CGRectMake(0, 170, 375, 50)];
    [self.view addSubview:self.number];
    self.number.Lable.text = @"学号:";
    self.number.TextField.text = self.stu.number;
    
    self.phone = [[LDLTView alloc] initWithFrame:CGRectMake(0, 230, 375, 50)];
    [self.view addSubview:self.phone];
    self.phone.Lable.text = @"电话:";
    self.phone.TextField.text = self.stu.phone;
    
    self.QQ = [[LDLTView alloc] initWithFrame:CGRectMake(0, 290, 375, 50)];
    [self.view addSubview:self.QQ];
    self.QQ.Lable.text = @"QQ:";
    self.QQ.TextField.text = self.stu.qq;
    
    self.address = [[LDLTView alloc] initWithFrame:CGRectMake(0, 350, 375, 50)];
    [self.view addSubview:self.address];
    self.address.Lable.text = @"籍贯:";
    self.address.TextField.text = self.stu.address;
    
    self.manager = [LDCoreDataManager defaults];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:button];
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 10;
    button.frame = CGRectMake(50, 430, 100, 50);
    [button setTitle:@"修改" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:mapButton];
    mapButton.layer.borderWidth = 1;
    mapButton.layer.cornerRadius = 10;
    mapButton.frame = CGRectMake(160, 430, 100, 50);
    [mapButton setTitle:@"地图" forState:UIControlStateNormal];
    [mapButton addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];

    
    
}

- (void)click1:(UIButton *)button {
    LDMapViewController *map = [[LDMapViewController alloc] init];
    map.str = self.address.TextField.text;
    map.delegate = self;
    
    [self.navigationController pushViewController:map animated:YES];
}

- (void)takeValue:(NSString *)str {
    if (str != nil) {
        self.address.TextField.text = str;
    }
    
}

- (void)click:(UIButton *)button {
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], self.stu.name];
    request.predicate = predicate;
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    Student *stu = [array lastObject];
    stu.name = self.name.TextField.text;
    stu.sex = self.sex.TextField.text;
    stu.qq = self.QQ.TextField.text;
    stu.number = self.number.TextField.text;
    stu.phone = self.phone.TextField.text;
    stu.address = self.address.TextField.text;
    [self.manager saveContext];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
